/**
 * 我的专案管理
 * @class MyProjectTreeGrid
 * @extends Ext.ux.tree.TreeGrid
 */
MyProjectTreeGrid = Ext.extend(Ext.ux.tree.TreeGrid, {
	assignMemberWindow: null,
	userId: null,
	constructor: function(config){
		Ext.apply(this,config);
		
		MyProjectTreeGrid.superclass.constructor.call(this, {
			id: 'my_project_tree_grid',
			width: 800,
			height: 300,
			border: false,
			enableDD: false,
			singleExpand: true,
			autoScroll: true,
			viewConfig: {
				forceFit: true
			},
	        columns: [{
	            header: "文件名称",
	            width: 280,
	            dataIndex: "name"
	        },{
	            header: "版本",
	            width: 40,
	            dataIndex: "version"
	        },{
	            header: "状态",
	            width: 85,
	            dataIndex: "status",
				tpl: new Ext.XTemplate('{[this.colorStatus(values.status)]}', {
					colorStatus: function(v) {
						if(v == "[500]On-Going") return "<font color=blue>"+v+"</font>";
		            	else if(v == "[100]Unstarted") return "<font color=gray>"+v+"</font>";
		            	else if(v == "[910]Closed"||v == "[900]Released") return "<font color=green>"+v+"</font>";
		            	else if(v == "[800]Delayed") return "<font color=red>"+v+"</font>";
		            	else return v;
					}
				})
	        },{
	            header: "负责人",
	            width: 115,
	            dataIndex: "owner"
	        },{
	            header: "申请日期",
	            width: 70,
	            dataIndex: "dateOfOrder"
	        },{
	            header: "发行日期",
	            width: 70,
	            dataIndex: "dateOfEffect"
	        },{
	            header: "备注",
	            width: 260,
	            dataIndex: "memo",
	            tpl: new Ext.XTemplate('{[this.renderStr(values.memo)]}', {
					renderStr: function(v) {
						return renderGridStr(v);
					}
				})
	        }],
	        dataUrl: '/pdm/category/asyncMyProject.htm?userId='+this.userId,
	        root: new Ext.tree.AsyncTreeNode({
	        	id: '0',
	            text: "root"
	        }),
	        tbar: [{
	        	id: 'assign_member',
	        	text: "负责人设置",
	        	iconCls: 'cls_assign_owner',
	        	scope: this,
	        	handler: function(){
	        		var cur_node = this.getSelectionModel().getSelectedNode();
	        		if(!cur_node){
	        			Ext.Msg.alert("温馨提示：","请先选择项目！");
	        		}else{
	        			var json = cur_node.toJsonString(null,
							function(key, val) {
								return (key == 'name'|| key == 'owner' || key == 'mastId' || key == 'tmplId' || key == 'status');
							}, {
								name: 'text',
								owner: 'owner',
								mastId: 'mastId',
								tmplId: 'tmplId',
								status: 'status'
							}
						);
						
			       		var obj = eval("("+json+")");
			       		
			       		if(obj.owner == "--" && obj.status == "--"){
			       			Ext.Msg.alert("提示：","文件处于搁置状态，无需分配负责人员！");
			       		}else{
			       			if(!this.assignMemberWindow){
			       				this.assignMemberWindow = new OwnerSelectionWindow({id: "win_owner_selection"});
			       			}
			       			this.assignMemberWindow.setTitle("对 ["+obj.text+"] 设置相关负责人");
			       			this.assignMemberWindow.show();
			       			
			       			var members = getCateOwnerList(cur_node.id, "emp");
			       			
			       			Ext.fly("_collectOwners").dom.value = members;
			       			this.assignMemberWindow.ownerSelectionPanel.ownerSelect.setReCosigners(members);
			       			this.assignMemberWindow.ownerSelectionPanel.ownerSelect.load();
			       			
			       		}
					}
	        	}
	        },"-",{
	        	text: "搁置",
	        	scope: this,
	        	tooltip: "指定文件不需要填写信息",
	        	handler: function(){
	        		var cur_node = this.getSelectionModel().getSelectedNode();
	        		if(!cur_node){
	        			Ext.Msg.alert("提示","请先进行选择！");
	        		}else{
		        		if(cur_node.isLeaf())
		        		{
							var json = cur_node.toJsonString(null,
								function(key, val) {
									return (key == 'name'|| key == 'owner'|| key == 'status');
								}, {
									name: 'text',
									owner: 'owner',
									status: 'status'
								}
							);
				       		var obj = eval("("+json+")");
				       		
				       		if(obj.owner=="--" && obj.status=="--"){
				       			Ext.Msg.alert("提示：","文件已处搁置状态！");
				       		}else{
				       			Ext.MessageBox.show({
									title: "搁置原因",
									msg: "请给出搁置文件["+obj.text+"]的原因！",
									width: 400,
									buttons: Ext.MessageBox.OKCANCEL,
									multiline: true,
									value: '',
									fn: disuseReason
								});
				       		}
				       		
						}else{
							Ext.Msg.alert("提示：","请选择具体文件再进行此操作！")
						}
					}
	        	}
	        },"-",{
	        	text: "恢复",
	        	scope: this,
	        	handler: function(){
	        		var cur_node = this.getSelectionModel().getSelectedNode();
					
					if(!cur_node){
	        			Ext.Msg.alert("提示","请先进行选择！");
	        		}else{
		        		var json = cur_node.toJsonString(null,
							function(key, val) {
								return (key == 'name'|| key == 'owner'|| key == 'status');
							}, {
								name: 'text',
								owner: 'owner',
								status: 'status'
							}
						);
			       		var obj = eval("("+json+")");
			       		
			       		if(obj.owner!="--" || obj.status!="--")
			       		{
			       			Ext.Msg.alert("提示：","文件已处于正常状态！");
			       		}else{
			       			Ext.MessageBox.show({
								title: "温馨提示：",
								msg: "您确认让["+obj.text+"]从搁置状态中恢复吗？",
								buttons: Ext.MessageBox.YESNO,
								fn: function(btn){
									if(btn == "yes")
									{
										Ext.Ajax.request({
											url: '/pdm/category/recoverCategory.htm',
											method: 'post',
											params: {
												cate_id: cur_node.id
											},
											success: function(response,options){										
												cur_node.parentNode.reload(true,true);
												/*
												var result = Ext.decode(response.responseText).message;
												Ext.Msg.alert("信息:", result);
												*/
											},
											failure: function(response){
												var result = Ext.decode(response.responseText).message;
												Ext.Msg.alert("信息:", result);
											}
										});
									}
									else return;
								}
							});
			       		}
	        		}
	        	}
	        },"->",{
	        	text: "刷新",
	        	iconCls: "cls_table_refresh",
	        	scope: this,
	        	handler: function(){
	        		this.root.reload(true,true);
	        	}
	        }]
		});
		
		this.on('click',function(node){
			document.getElementById("_selected_pro_id").value = node.id;
	    });
	    
	    this.on('dblclick',function(node){
	    	if(node.isLeaf()){
	    		var json = node.toJsonString(null,
					function(key, val) {
						return (key == 'name'|| key == 'owner' || key == 'mastId' || key == 'tmplId');
					}, {
						name: 'text',
						owner: 'owner',
						mastId: 'mastId',
						tmplId: 'tmplId'
					}
				);
	       		var obj = eval("("+json+")");
	       		
	       		if(obj.owner == "--"){
	       			Ext.Msg.alert("温馨提示：","文件["+obj.text+"]不需要维护！");
	       		}
	       		else
	       		//window.open("AutoCreate.jsp?mast_id="+obj.mastId+"&tmpl_id="+obj.tmplId, "", "height=600px, width=800px, toolbar=no, menubar=yes, scrollbars=yes, resizable=yes, status=yes");
	       		window.open(window.basePath + "/form.htm?method=show&mast_id="+obj.mastId);
	    	}
	    });
	    
	    this.on('contextmenu',function(node, e){
			/*
			node.select();
			var c = new Ext.menu.Menu({
				items: [{
					text: 'Add Node'
				}],
				listeners: {
					itemclick: function(item) {
							alert(node.id);
						}
			    }
			});
			c.showAt(e.getXY());
			*/
	    });
	}
});

function disuseReason(btn, text){
	if(btn == 'ok')
	{
		if(text == '')
		{
			Ext.MessageBox.confirm("温馨提示", "您需要给出搁置的原因！",
				function(btn){
					if(btn == 'yes')
					{
						Ext.MessageBox.show({
							title: "搁置原因",
							msg: "请给出搁置此份文件的原因！",
							width:400,
							buttons: Ext.MessageBox.OKCANCEL,
							multiline: true,
							value: '',
							fn: disuseReason
						});
					}else return;
　　　　　　　　　　});
		}else{
			var my_project_tree_grid = Ext.getCmp("my_project_tree_grid");
			var cur_node = my_project_tree_grid.getSelectionModel().getSelectedNode();
		
			Ext.Ajax.request({
				url: '/pdm/category/disuseCategory.htm',
				method: 'post',
				params: {
					cate_id: cur_node.id,
					memo: text
				},
				success: function(response,options){
					cur_node.parentNode.reload(true,true);
					
				},
				failure: function(response){
					var result = Ext.decode(response.responseText).message;
					Ext.Msg.alert("信息:", result);
				}
			});
		}
	}
	else if(btn == 'cancel') return;
}


/**
 * 文档总览表
 * @class ProjectList
 * @extends Ext.ux.tree.ColumnTree
 */
ProjectList = Ext.extend(Ext.ux.tree.ColumnTree, {
	cln_width: null,
	cln_heigth: null,
	condition: null,
	loader: null,
	constructor: function(config){
		Ext.apply(this, config);
		/*
		* @cln_width    获取浏览器当前窗口宽度
		* @cln_heigth   获取浏览器当前窗口高度
		*/
		this.cln_width = document.body.clientWidth-225;
		this.cln_heigth = document.body.clientHeight-100;
		
		this.condition = {cate_Id: 40};
		
		this.loader = new Ext.tree.TreeLoader({
			baseParams: this.condition,
            dataUrl: window.basePath + '/project.htm?method=list',
            uiProviders:{
                'col': Ext.ux.tree.ColumnNodeUI
            }
        });
		
		ProjectList.superclass.constructor.call(this, {
			id: 'allProjectList',
			width: this.cln_width,
	        height: this.cln_heigth*0.87,
	        rootVisible: false,
	        useArrows: false,
	        lines: true,
	        border: false,
	        enableDD: false,
	        autoScroll: true,
	        title: "&nbsp;",
	        viewConfig: {forceFit: true},
	        columns:[{
	            header: "文件名称",
	            width: this.cln_width*0.3,
	            dataIndex: 'resource'
	        },{
	            header: "状态",
	            width: this.cln_width*0.11,
	            dataIndex: 'status',
	            renderer: function(v){
	            	if(v == "[500]On-Going") return "<font color=blue>"+v+"</font>";
	            	else if(v == "[100]Unstarted") return "<font color=gray>"+v+"</font>";
	            	else if(v == "[910]Closed"||v == "[900]Released") return "<font color=green>"+v+"</font>";
	            	else if(v == "[800]Delayed") return "<font color=red>"+v+"</font>";
	            	else return v;
	            }
	        },{
	            header: "负责人",
	            width: this.cln_width/10,
	            dataIndex: 'owner'
	        },{
	            header: "申请日期",
	            width: this.cln_width/10,
	            dataIndex: 'createDate'
	        },{
	            header: "发行日期",
	            width: this.cln_width/10,
	            dataIndex: 'issueDate'
	        },{
	            header: "版本",
	            width: this.cln_width/20,
	            dataIndex: 'version'
	        },{
	            header: "备注",
	            width: this.cln_width*0.22,
	            dataIndex: 'memo'
	        }],
	        loader: this.loader,
	        root: new Ext.tree.AsyncTreeNode({
	        	id: '40',
	            text: 'resource'
	        })
		});
		this.on('dblclick',function(node){
			Ext.fly("_selected_node_id").dom.value = node.id;
			
			if(node.isLeaf())
			{
				var selectedItem = this.getSelectionModel().getSelectedNode();
				var json = node.toJsonString(null,
					function(key, val) {
						return (key == 'mastId'|| key == 'owner');
					}, {
						mastId: 'mastId',
						owner: 'owner'
					});
	       		var obj = eval("("+json+")");
	       		
	       		if(obj.mastId == 0) Ext.Msg.alert("提示:","表单无法打开，请联系管理员！");
	       		//else window.open("AutoCreate.jsp?mast_id="+obj.mastId+"&tmpl_id="+obj.tmplId);
	       		else window.open(window.basePath + "/form.htm?method=show&mast_id="+obj.mastId);
			}
			
	    });
	},
	load: function(){
		this.condition.cate_Id = 40;
		this.loader.root.reload();
	}
});
	